

export function getCache(key: string) {
  return new Promise((yes, err) => {
    try {
      let funId = "getCache_" + rand(32)
      ipcRenderer.on(funId, (event, message) => {
        ipcRenderer.off(funId, (e) => { })
        yes(message);
      });
      ipcRenderer.send("getCache", {
        key, //文件名称
        funId
      });
    } catch (error) {
      err(error)
    }
  })
}


export function setCache(key: string, value: string) {
  return new Promise((yes, err) => {
    try {
      let funId = "setCache_" + rand(32)
      ipcRenderer.on(funId, (event, message) => {
        ipcRenderer.off(funId, (e) => { })
        yes(message);
      });
      ipcRenderer.send("setCache", {
        key, //文件名称
        value, //文件内容
        funId
      });
    } catch (error) {
      err(error)
    }
  })
}



//len为随机数长度，这个函数随机生成数字和字母
function rand(len: number) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';

  for (let i = 0; i < len; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }

  return result;
}